home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6091 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.8 KB  |  86 lines

  1. Path: ub239.dialup.uwa.edu.au!localhost!prye
  2. From: prye@ub239.dialup.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Help] I can't find my error.
  5. Date: 22 Feb 1996 14:47:59 GMT
  6. Organization: The University of Western Australia
  7. Message-ID: <PRYE.96Feb22224759@ub239.dialup.uwa.edu.au>
  8. References: <4ggvgr$1b2@aurora.engr.LaTech.edu>
  9. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  10. In-reply-to: pluu@engr.LaTech.edu's message of 22 Feb 1996 05:38:35 GMT
  11.  
  12. >>>>> "PL" == PL  <pluu@engr.LaTech.edu> writes:
  13.  
  14.     PL> Hi, all; Could anyone please tell me why the following program
  15.     PL> won't work?
  16.  
  17.  
  18.     PL> /* This program will ask your name and age; and then will print
  19.     PL> your name and age for next year back */
  20.  
  21.     PL> #include <stdio.h>
  22.  
  23.     PL> main() { char name; 
  24.     PL>         int age, next_age;
  25.  
  26.     PL>     printf("%s\n","Please enter your name and age: ");
  27.     PL>     scanf("%s%d\n", &name, &age); 
  28.     PL>        next_age= age + 1; 
  29.     PL>    printf("Hi, %s ,next year, you will be %d\n", name, next_age); 
  30.     PL>    }
  31.  
  32. Hi PL,
  33.  
  34. Have a look at this.
  35. scanf is notorious for being difficult to use for parsing user input.
  36. This is discussed in considerable detail in the FAQ (Section 12).
  37. You will see much advice about getting user input using fgets, and then
  38. parsing the string obtained with sscanf if you read a few of the messages
  39. here (also in the FAQ in the same section).
  40.  
  41. A couple of other points are that you only declared name to be a single
  42. char.....probably not what you wanted. If the user enters his/her full 
  43. name it will obviously considerably longer and not fit into the allocated
  44. storage. I have arbitrarily chosen an array size of 64 here. Since name is
  45. an array, it will be passed to sscanf as a pointer to the array and does
  46. not need the preceding ampersand.
  47.  
  48. /* This program will ask your name and age; and then
  49. will print your name and age for next year back */
  50.  
  51. #include <stdio.h>
  52. #include <stdlib.h>
  53.  
  54. int main()
  55. {
  56.         char   name[64];        /* you need storage for name */
  57.     char answer[64];
  58.     int  age, next_age, converted;
  59.  
  60.     printf("Please enter your name and age: \n");
  61.     fgets(answer, 64, stdin);
  62.  
  63.     converted = sscanf(answer,"%s%d\n", name, &age);
  64.  
  65.     if (converted != 2) {        /* if scanf fails to convert 2   */
  66.       puts("Bad input");        /* variables on input, assume    */
  67.       exit(1);            /* bad input and exit with error */
  68.     }
  69.  
  70.     next_age= age + 1;
  71.     printf("Hi, %s ,next year, you will be %d\n", name, next_age);
  72.  
  73.     return 0;            /* main returns an int */
  74. }
  75.  
  76. Hope this is of some help,
  77.  
  78. Peter Rye
  79.  
  80.  
  81. --
  82. | Peter Rye                                 Smoking areas in restaurants |
  83. | Respiratory Research Fellow               are like peeing areas in     |
  84. | Princess Margaret Hospital for Children   swimming pools.              |
  85. | Perth, Western Australia                                               |
  86.